#include <iostream>
#include <string> 
using namespace std;
template <typename T>
inline void read(T& x)
{
    int c=getchar(), f=1; x=0;
    while(c<'0'||'9'<c) {if(c=='-') f=-1; c=getchar();}
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    x*=f;
}
template <typename T>
inline void quickwrite(T x)
{
    if(x>=10) quickwrite(x/10);
    putchar(x%10+'0');
}
#define DEBUG
using ll=long long;
ll daka, day, consday, blank;
bool flag=0;
inline ll qmi(ll base, ll index)
{
    ll res=1;
    while(index)
    {
        if(index&1) res=(res*base);
        index>>=1;
        base=base*base;
    }
    return res;
}

void init()
{
    string line; getline(cin, line);
}

void solve()
{
    init();
    while(cin >> daka)
    {
        if(daka)
        {  
            // if(blank) consday=max(consday-qmi(2, blank-1), 1ll*0);
            ++consday; 
            blank=1; flag=true; //惩罚机制初始化
        }
        else
        {
            consday-=blank;
            if(blank==1&&flag) blank=1, flag=false;
            else blank<<=1;
        } 
        if(consday<0) consday=0;
    }
    quickwrite(consday); puts("");
}

// #undef DEBUG
signed main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif

    int T=1; //scanf("%d", &T);
    while(T--) 
    {
        solve();
    }
    return 0;
}